package protocol.SZ_2021;

import java.util.HashMap;

public class Handle2021SZ92 implements IHandle2021SZ {
    @Override
    public String handle(SZ_2021Cmd cmd) {
        byte subOrder = cmd.subOrder();

        switch (subOrder){
            case 0x05:
                return handle9205(cmd);
            case 0x07:
                return handle9207(cmd);
            case 0x12:
                return handle9212(cmd);
            case 0x18:
                return handle9218(cmd);
            default:
                return "深标:未匹配的命令字"+(cmd.readOrder()+","+cmd.readSubOrder());
        }
    }

    private String handle9212(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("终端->平台:文件上传完成消息应答");
        builder.append("\r\n");
        builder.append("文件名称:"+cmd.readString(cmd.readInt(1)));
        builder.append("\r\n");

        HashMap<Integer,String> list = new HashMap<>();
        list.put(0,"图片");
        list.put(1,"音频");
        list.put(2,"视频");
        list.put(3,"文本");
        list.put(4,"其他");
        builder.append("文件类型:"+list.getOrDefault(cmd.readInt(1),"未定义"));
        builder.append("\r\n");

        list.clear();
        list.put(0,"完成");
        list.put(1,"需要补传");


        int size = cmd.readInt(1);
        builder.append("补传数据包数量:"+size);
        builder.append("\r\n");

        for (int i = 0; i < size; i++) {
            builder.append("    数据偏移量:"+cmd.readDWord());
            builder.append("\r\n");
            builder.append("    数据长度:"+cmd.readDWord());
            builder.append("\r\n");
        }

        return builder.toString();
    }

    private String handle9218(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:报警附件下载");
        builder.append("\r\n");

        int size = cmd.readInt(1);
        builder.append("服务器IP地址长度："+size);
        builder.append("\r\n");
        builder.append("服务器IP地址："+cmd.readString(size));
        builder.append("\r\n");
        builder.append("TCP 端口："+cmd.readWord());
        builder.append("\r\n");
        builder.append("UDP 端口："+cmd.readWord());
        builder.append("\r\n");
        cmd.moveIndex(16);

        builder.append("报警编号："+cmd.readBCD(32));
        builder.append("\r\n");
        cmd.moveIndex(16);

        int num = cmd.readInt(1);
        builder.append("报警附件数量："+num);
        builder.append("\r\n");
        for (int i=0;i<num;i++){

            int type = cmd.readInt(1);
            HashMap<Integer,String> typeList = new HashMap<>();
            typeList.put(0,"音视频");
            typeList.put(1,"音频");
            typeList.put(2,"视频");
            typeList.put(3,"视频或音频");
            typeList.put(4,"车辆状态数据记录");

            builder.append("    文件类型("+type+")："+typeList.getOrDefault(type,"未定义"));
            builder.append("\r\n");

            int stream = cmd.readInt(1);
            HashMap<Integer,String> streamList = new HashMap<>();
            streamList.put(0,"所有码流");
            streamList.put(1,"主码流");
            streamList.put(2,"子码流");

            builder.append("    码流类型("+stream+")："+streamList.getOrDefault(stream,"未定义"));
            builder.append("\r\n");

            int format = cmd.readInt(1);
            HashMap<Integer,String> formatList = new HashMap<>();
            if (type == 0||type == 1||type == 2){
                formatList.put(2,"H264");
            }else if (type == 3) {
                formatList.put(0, "JPEG");
            }

            builder.append("    文件格式("+format+")："+formatList.getOrDefault(format,"未定义"));
            builder.append("\r\n");

            int storage = cmd.readInt(1);
            HashMap<Integer,String> storageList = new HashMap<>();
            storageList.put(0,"主存储器或灾备存储器");
            storageList.put(1,"主存储器");
            storageList.put(2,"灾备存储器");
            builder.append("    存储器类型("+storage+")："+storageList.getOrDefault(storage,"未定义"));
            builder.append("\r\n");

            builder.append("    逻辑通道号："+cmd.readInt(1));
            builder.append("\r\n");

            builder.append("    开始时间："+cmd.readBCD(6));
            builder.append("\r\n");
            builder.append("    结束时间："+cmd.readBCD(6));
            builder.append("\r\n");

            if (type == 3){
                builder.append("    图片张数："+cmd.readInt(1));
                builder.append("\r\n");
            }else{
                cmd.moveIndex(1);
            }
            builder.append("\r\n");

        }

        return builder.toString();
    }

    private String handle9207(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:文件上传控制");
        builder.append("\r\n");

        builder.append("应答流水号："+cmd.readWord());
        builder.append("\r\n");

        int type = cmd.readInt(1);
        HashMap<Integer,String> typeList = new HashMap<>();
        typeList.put(0,"暂停");
        typeList.put(1,"继续");
        typeList.put(2,"取消");

        builder.append("上传控制("+type+")："+typeList.getOrDefault(type,"未定义"));
        builder.append("\r\n");

        return builder.toString();
    }

    private String handle9205(SZ_2021Cmd cmd) {
        StringBuilder builder = new StringBuilder();
        builder.append("平台->终端:文件上传指令");
        builder.append("\r\n");

        int size = cmd.readInt(1);
        builder.append("服务器IP地址长度："+size);
        builder.append("\r\n");
        builder.append("服务器IP地址："+cmd.readString(size));
        builder.append("\r\n");
        builder.append("端口："+cmd.readWord());
        builder.append("\r\n");
        builder.append("用户名："+cmd.readString(cmd.readInt(1)));
        builder.append("\r\n");
        builder.append("密码："+cmd.readString(cmd.readInt(1)));
        builder.append("\r\n");
        builder.append("文件上传路径："+cmd.readString(cmd.readInt(1)));
        builder.append("\r\n");

        builder.append("逻辑通道号："+cmd.readInt(1));
        builder.append("\r\n");

        builder.append("开始时间："+cmd.readBCD(6));
        builder.append("\r\n");
        builder.append("结束时间："+cmd.readBCD(6));
        builder.append("\r\n");

        builder.append("报警标识1："+cmd.readBCD(4));
        builder.append("\r\n");
        builder.append("报警标识2："+cmd.readBCD(4));
        builder.append("\r\n");

        int type = cmd.readInt(1);
        HashMap<Integer,String> typeList = new HashMap<>();
        typeList.put(0,"音视频");
        typeList.put(1,"音频");
        typeList.put(2,"视频");
        typeList.put(3,"视频或音频");

        builder.append("音视频类型("+type+")："+typeList.getOrDefault(type,"未定义"));
        builder.append("\r\n");

        int stream = cmd.readInt(1);
        HashMap<Integer,String> streamList = new HashMap<>();
        streamList.put(0,"所有码流");
        streamList.put(1,"主码流");
        streamList.put(2,"子码流");

        builder.append("码流类型("+stream+")："+streamList.getOrDefault(stream,"未定义"));
        builder.append("\r\n");

        int storage = cmd.readInt(1);
        HashMap<Integer,String> storageList = new HashMap<>();
        storageList.put(0,"主存储器或灾备存储器");
        storageList.put(1,"主存储器");
        storageList.put(2,"灾备存储器");
        builder.append("存储器类型("+storage+")："+storageList.getOrDefault(storage,"未定义"));
        builder.append("\r\n");

        int condition = cmd.readInt(1);
        builder.append("任务执行条件："+condition);
        builder.append("\r\n");

        builder.append("    WIFI状态："+(((condition&0x01) == 0x01)?"可用":"不可用"));
        builder.append("\r\n");
        builder.append("    LAN状态："+(((condition>>1&0x01) == 0x01)?"可用":"不可用"));
        builder.append("\r\n");
        builder.append("    3G/4G状态："+(((condition>>2&0x01) == 0x01)?"可用":"不可用"));
        builder.append("\r\n");

        return builder.toString();
    }


}
